package src.tree.level_order;

import src.tree.TreeNode;

import java.util.*;

public class no515 {//在每个树行中找最大值

    public List<Integer> largestValues(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) return res;
        queue.add(root);

        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            int max = Integer.MIN_VALUE;

            while (size-- > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                max = Math.max(max, node.val);
                if (node.left != null) queue.add(node.left);
                if (node.right != null) queue.add(node.right);
            }
            //res.add(Collections.max(list));
            res.add(max);
        }
        return res;

    }

}
